package cn.iocoder.yudao.module.cmt.service.venue;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.venue.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.venue.VenueTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.venue.VenueTypeMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class VenueTypeServiceImpl implements VenueTypeService {

    @Resource
    private VenueTypeMapper venueTypeMapper;

    @Override
    public Long createVenueType(VenueTypeSaveReqVO createReqVO) {
        VenueTypeDO venueType = BeanUtils.toBean(createReqVO, VenueTypeDO.class);
        venueTypeMapper.insert(venueType);
        return venueType.getId();
    }

    @Override
    public void updateVenueType(VenueTypeSaveReqVO updateReqVO) {
        validateVenueTypeExists(updateReqVO.getId());
        VenueTypeDO updateObj = BeanUtils.toBean(updateReqVO, VenueTypeDO.class);
        venueTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteVenueType(Long id) {
        validateVenueTypeExists(id);
        venueTypeMapper.deleteById(id);
    }

    private void validateVenueTypeExists(Long id) {
        if (venueTypeMapper.selectById(id) == null) {
            throw exception(VENUE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public VenueTypeDO getVenueType(Long id) {
        return venueTypeMapper.selectById(id);
    }

    @Override
    public PageResult<VenueTypeDO> getVenueTypePage(VenueTypePageReqVO pageReqVO) {
        return venueTypeMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVenueTypeListByIds(List<Long> ids) {
        ids.forEach(this::validateVenueTypeExists);
        venueTypeMapper.deleteBatchIds(ids);
    }

}
